package es.uc3m.setichat.android.activity;


import android.app.Activity;
import android.app.ListFragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import es.uc3m.setichat.android.SeTIChatService;


/**
 * Activity that list a number of SeTIChat contacts.
 * 
 * Notice that this window should only list SeTIChat contacts and not telephone contacts.
 * 
 * @author Guillermo Suarez de Tangil <guillermo.suarez.tangil@uc3m.es>
 * @author Jorge Blasco Alis <jbalis@inf.uc3m.es>
 */

public class SeTIChatContactsActivity extends Activity {
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // During initial setup, plug in the contacts fragment.
        SeTIChatContactsFragment contacts = new SeTIChatContactsFragment();
        contacts.setArguments(getIntent().getExtras());
        getFragmentManager().beginTransaction().add(android.R.id.content, contacts).commit();
    }
    
    
    
    /**
     * This is the "top-level" fragment, showing a list of items that the
     * user can pick.  Upon picking an item, it takes care of displaying the
     * data to the user as appropriate based on the currrent UI layout.
     */
    public static class SeTIChatContactsFragment extends ListFragment {

    	private boolean DEBUG = false;
    	private SeTIChatService mBoundService;
    	public static SeTIChatContactsFragment fragment = null;
    	

    	private ServiceConnection mConnection = new ServiceConnection() {
    		public void onServiceConnected(ComponentName className, IBinder service) {
    	        // This is called when the connection with the service has been
    	        // established, giving us the service object we can use to
    	        // interact with the service.  Because we have bound to a explicit
    	        // service that we know is running in our own process, we can
    	        // cast its IBinder to a concrete class and directly access it.
    	        mBoundService = ((SeTIChatService.LocalBinder)service).getService();
    	        
    	        DEBUG = mBoundService.getDebug();
    	        
                render();
    	    }

    	    public void onServiceDisconnected(ComponentName className) {
    	        // This is called when the connection with the service has been
    	        // unexpectedly disconnected -- that is, its process crashed.
    	        // Because it is running in our same process, we should never
    	        // see this happen.
    	    	
    	    	if(DEBUG)
    	        	Log.d("ContactsFragment", "onServiceDisconnected: un-bounding the service");
    	    	
    	    }
    	};

    	
    	
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            fragment = this;
            if(mBoundService == null){
	            //Binding the activity to the service to get shared objects
	            if(DEBUG)
	            	Log.d("SeTIChatConversationActivity", "Creating activity");
	            getActivity().bindService(new Intent(getActivity(), SeTIChatService.class), mConnection, Context.BIND_AUTO_CREATE);
            }else{

                render();
            }
        } 
        
        @Override
		public void onStop(){
        	super.onStop();
        	fragment = null;
        	if(mBoundService != null){
        		mBoundService.stopSelf();

            	if(DEBUG)
                	Log.d("SeTIChatConversationActivity", "Stopping activity");
        	}
        	
        }
        
    	@Override
		public void onPause() {
    		super.onPause();
    		fragment = null;
    	}
    	
    	@Override
		public void onResume() {
    		super.onResume();
    		fragment = this;
    	}
    	
    	
    	@Override
    	public void onDestroy() {
    		super.onDestroy();
    		fragment = null;
    	}
        
        public void render() {
        	
        	//Populate list with contacts.
        	//Ey, a more fancy layout could be used! You dare?!
        	Log.d("XMPP-Receive","estoy en el render");
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_activated_1, 
                    mBoundService.contactsController.printContacts()));   
		}

		@Override
        public void onListItemClick(ListView l, View v, int position, long id) {
        	// We need to launch a new activity to display
            // the dialog fragment with selected text.
			
			if(mBoundService == null){
				Log.d("traa","service es null");
			}else{
				Log.d("traa","service no es null");
			}
			
			if(mBoundService.contactsController == null){
				Log.d("traa","contacts es null");
			}else{
				Log.d("traa","contacts no es null");
			}
			
			if(mBoundService.contactsController.actual_contacts == null){
				Log.d("traa","es null");
			}else{
				Log.d("traa","no es null");
			}
			Log.d("traa",String.valueOf(position));
			Log.d("traa",mBoundService.contactsController.actual_contacts.get(position).getNick());
			if(DEBUG)
            	Log.d("SeTIChatConversationActivity","mobile="+mBoundService.contactsController.actual_contacts.get(position).getMobile());
            
			Intent intent = new Intent();
            intent.setClass(getActivity(), SeTIChatConversationActivity.class);
            intent.putExtra("index", position);
            intent.putExtra("mobile", mBoundService.contactsController.actual_contacts.get(position).getMobile());
            startActivity(intent);
            
        }
        
    }
    
}